﻿//using UnityEngine;
//using System.Collections;
//using System.Collections.Generic;
//using BestHTTP;
//using System;
//using System.IO;
//using LitJson;
//using System.Security.Cryptography;
//using HLLIB_ASSETBUNDLE;
//using DataDefine;
//public class HttpDownAssetbundle : MonoBehaviour
//{

//	private const string address = "http://192.168.0.116:8080/test/StreamingAssets/";  //地址
//	private string txtaddress = address + "version.txt"; // 版本号的地址~
//	private string jsonaddress = address + "PersistentInfo.json"; // 所有需要下载的资源表
//	private string loacladdress = "/loaclversion.txt";   // 本地保存的版本号
//	//大版本号
//	private static int LargeVersion_number;
//	//中版本号
//	private static int Middleersion_number;
//	//小版本号
//	private static int SmallVersion_number;
//	// false 版本号不一致 ，ture 版本号一致
//	private bool   versionbvalue = false;
//	private  string  AssetFile = "/YLSJAssetBundles";
//	public static float  progress;
//	//进度条了哇
//	public UISlider ProgressBar;
//	public UILabel Progresstext;
//	public GameObject AgainTip;
//	private UILabel Showtiperror;
//	private GameObject button;
//	private bool Network=false;
//	//创建文件夹的目录
//	private static  string[] ToobalTitle = new string[] {
//		"Atlas",
//		"Audio",
//		"OtherAsset",
//		"DataTable",
//		"Effects",
//		"ExtScripts",
//		"Fonts",
//		"Modles",
//		"Scenes",
//		"Textures",
//		"UISystem"
//	};	
//	enum LoadType
//	{
//		txttype,
//		jsontype,
//		assetbundletupe,
//	}	
//	public bool Booleanga = false;
//	public static HttpDownAssetbundle Instancel;
//	public static  bool _godownload=false;
//	// Use this for initialization
	
//	void Awake()
//	{
//		Instancel = this;
//		if (_godownload) 
//		{
//			GameScreen.isLoadAssetBundleFromStreamingAssets = false;
//			loacladdress = Application.persistentDataPath + loacladdress;
//			AssetFile = Application.persistentDataPath + AssetFile;
//			AssetbundleManager.instance.Readwhichpath ();
//			DownLoadVersion ();
//		} 
//		else 
//		{
//			GameScreen.isLoadAssetBundleFromStreamingAssets = true;
//			AssetbundleManager.instance.Readwhichpath ();
//			//StartCoroutine (Gointoaction ());
//			//		return;	
//		}
//	}
	
//	void Start ()
//	{
//		button=AgainTip.transform.FindChild("Button").gameObject;
//		UIEventListener.Get (button.gameObject).onClick = ButtonOnclick;

//		Showtiperror=AgainTip.transform.FindChild("Showtip").GetComponent<UILabel>();
//		TabelManager();
//	//	Instancel = this;
////	//	if (AppInit.instance._godownload) {
////			GameScreen.isLoadAssetBundleFromStreamingAssets = false;
////			loacladdress = Application.persistentDataPath + loacladdress;
////			AssetFile = Application.persistentDataPath + AssetFile;
////			DownLoadVersion ();
////		
////	//	} else {
////			GameScreen.isLoadAssetBundleFromStreamingAssets = true;
////			StartCoroutine (Gointoaction ());
////		    AssetbundleManager.instance.Readwhichpath ();
////			return;
////	//	}
		
//	}
	
//	void Update ()
//	{
//		float value = (float)(progress / 100.0);
//		if (ProgressBar.value != value) {
//			ProgressBar.value = Mathf.Lerp (ProgressBar.value, value, Time.deltaTime * 3); 
//			Progresstext.text = (ProgressBar.value * 100).ToString () + "/" + "100";
//		}
//		if (ProgressBar.value >= 1 && Booleanga == false) {
//			Booleanga = true;
//			StartCoroutine (Gointoaction ());
//		}
//	}
     
     
//	IEnumerator  Gointoaction ()
//	{
//		yield return new WaitForSeconds (0.1f);
//		AppInit.instance.startGame ();
		
//	}
		
//	/// <summary>
//	/// Downs the load version. 下载服务器的版本号！！
//	/// </summary>
//	private void DownLoadVersion ()
//	{
//		StartCoroutine (DownLoadAssetbundle (txtaddress, LoadType.txttype));
//	}
	
	
//	/// <summary>
//	/// Loacls the version text.  是否曾经下载过或者用于比较和服务器一样吗
//	/// </summary>
//	/// <returns>The version text.</returns>
//	private string LoaclVersionTxt ()
//	{
//		if (File.Exists (loacladdress)) {
//			StreamReader sr = new StreamReader (loacladdress);
//			String line = sr.ReadToEnd ();
//			return line.Replace (Environment.NewLine, "");
//		}
//		return null;
//	}
	
//	/// <summary>
//	/// Judges the version. 判断版本号是否一致
//	/// </summary>
//	private void  JudgeVersion (string txtcontent)
//	{
//		string[] temp_server = txtcontent.Split ('.');
//		string[] loacl_server = new string[]{};
//		if (LoaclVersionTxt () != null) {
//			loacl_server = LoaclVersionTxt ().Split ('.');
//		}
//		//	string[] loacl_server= LoaclVersionTxt ().Split('.');
////		for (int i = 0; i < temp_server.Length; i++) {
////			if (temp_server [i] != null) {
////				LargeVersion_number = int.Parse (temp_server [0]);
////				Middleersion_number = int.Parse (temp_server [1]);
////				SmallVersion_number = int.Parse (temp_server [2]);
////			}	
////		}
////       这里是留区分大中小三个版本号使用的  ，现在只要版本号不同就更新。
//		if (loacl_server.Length > 0) {
////			if(temp_server[0]==loacl_server[0])
////			{
////				
////			}
//			versionbvalue = String.Equals (LoaclVersionTxt (), txtcontent.Replace (Environment.NewLine, ""));
//		} else { // 证明第一次下载啊
//			versionbvalue = false;
//		}
	
//		// 不一致的情况
//		if (versionbvalue == false) {
//			GreatLocalFile ();
//			DownLoadJson ();
//			StorageWriteTxt (txtcontent);
//		} else {
//			DownLoadJson ();
//		}
//	}          
	
	
//	/// <summary>
//	/// Writes the text.
//	/// </summary>存储一个本地的版本号
//	private void  StorageWriteTxt (string line)
//	{
//		if (File.Exists (loacladdress)) {
//			File.Delete (loacladdress);
//		}
//		FileStream yl = new FileStream (loacladdress, FileMode.Create);
//		StreamWriter Sw = new StreamWriter (yl);
//		Sw.WriteLine (line);
//		Sw.Flush ();
//		Sw.Close ();
//	}
	
	
//	/// <summary>
//	/// Greats the local file.  创建本地的文件夹
//	/// </summary>
//	private void GreatLocalFile ()
//	{  
//		if (Directory.Exists (AssetFile)) {
//			Directory.Delete (AssetFile);
//		}
	
//		if (!Directory.Exists (AssetFile)) {
//			Directory.CreateDirectory (AssetFile);
//		}
//		for (int i = 0; i < ToobalTitle.Length; i++) {
//			string path = AssetFile + "/" + ToobalTitle [i];
//			if (!Directory.Exists (path)) {
//				Directory.CreateDirectory (path);
//			}
//		}
//	}
	
	
//	/// <summary>
//	/// Downs the load json. 版本号不一样！下载json表格下载资源
//	/// </summary>
//	private void DownLoadJson ()
//	{
//		StartCoroutine (DownLoadAssetbundle (jsonaddress, LoadType.jsontype));
//	}

//	private int totalResource;
//	private List<ResourcesPath> jsonassetbundledata = new List<ResourcesPath> ();
//	/// <summary>
//	/// Analysises the json.
//	/// </summary>解析存储的json  打包的数据
//	/// <param name="jsonpath">Jsonpath.</param>
//	private void AnalysisJson (string jsonpath)
//	{
//		DownLoadJsonTabel jsondata = JsonMapper.ToObject<DownLoadJsonTabel> (jsonpath);
//		totalResource = jsondata.StreamingAssetsinfo.Count;
//		if (jsondata == null) {
//			return; 
//		}
//		foreach (ResourcesPath item in jsondata.StreamingAssetsinfo) {
//			if (!jsonassetbundledata.Contains (item)) {
//				jsonassetbundledata.Add (item);
//			}
//		}
//		if (versionbvalue == false) {    // 如果版本号不一样的情况
//			StartCoroutine (AssetbundleDownLoad ());
//		} else {                        // 如果一样请比较？MD5
//			ComparisonversionMD5 ();
//		}
		
//	}
	
	
//	/// <summary>
//	/// Assetbundles down load. 下载主要资源哇 ,下载完整之后比较MD5
//	/// </summary>
//	IEnumerator  AssetbundleDownLoad ()
//	{
//		for (int i = 0; i < jsonassetbundledata.Count; i++) {
//			string date = address + jsonassetbundledata [i].Path_name;
//			yield return  StartCoroutine (DownLoadAssetbundle (date, LoadType.assetbundletupe));
//			//	Debug.Log ("-------------------------------");
//		}
//	//	Debug.Log("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
//		// 下载完成之后比对MD5值
//		if(DownAdress.Count==totalResource)
//		{
//			ComparisonversionMD5 ();
//		}
		
//	}
	
	
//	/// <summary>
//	/// Greats the asset bundle file.  下载本地保存 
//	/// </summary>
//	/// <param name="Bytes">Bytes.</param>
//	/// <param name="path">Path.</param>
//	IEnumerator GreatAssetBundleFile (byte[] Bytes, string path)
//	{
////	Debug.Log(path+"==============================="+Bytes);
//		if (Bytes.Length <= 0)
//			yield break;
//		string m_name = "StreamingAssets";
//		int ad = path.LastIndexOf (m_name);
//		string temp = path.Substring (ad + m_name.Length);
//		string data_path = AssetFile + temp;
//		yield return Bytes;
//		byte[] W_Bytes = Bytes;
//		if (W_Bytes.Length > 0) {
//			FileStream yl = new FileStream (data_path, FileMode.Create);
//			yl.Write (W_Bytes, 0, W_Bytes.Length);
//			yl.Flush ();
//			yl.Close ();
//		} else {
//			Debug.Log ("AssetBundle LoadSceneCoroutine is error <" + ">");
//		}
//		W_Bytes = null;
//	}
	
	
//	/// <summary>
//	/// Sameversion this instance.比对MD5 值， 一样过 ，不一样重新下载啦
//	/// </summary>
//	private void  ComparisonversionMD5 ()
//	{
//	    // 如果没有下载完毕不要比较MD5
		
//		for (int i = 0; i <jsonassetbundledata.Count; i++) {
//			string newdatepath = AssetFile + "/" + jsonassetbundledata [i].Path_name;
//		//	Debug.Log("---------------------------"+newdatepath);
//			//	Debug.Log (totalResource + "ddddd" + jsonassetbundledata.Count + "dddddd" + versionbvalue + "dddddddddddddddddddddddddddddddddddd" + newdatepath);
//			if (File.Exists (newdatepath)) {

//				string newMD = ComparisonMD5 (newdatepath);
//				string oldMD = jsonassetbundledata [i].MD_Vlave;
//				bool eqbool = String.Equals (newMD, oldMD);
//				if (eqbool) {
//					if (versionbvalue == true) {
//						progress = progress + (float)(101.0 / totalResource);
//					} else {
//						progress = progress + (float)(21.0 / totalResource);
//					}
					
//				} else {
//					string date = address + jsonassetbundledata [i].Path_name;
//					StartCoroutine (DownLoadAssetbundle (date, LoadType.assetbundletupe));
//				}
//			} else {
//				string date = address + jsonassetbundledata [i].Path_name;
//				StartCoroutine (DownLoadAssetbundle (date, LoadType.assetbundletupe));
//			}
//		}
//	}
	
	

//	/// <summary>
//	/// Down adress. 已经下载过的地址
//	/// </summary>
//	public List<string> DownAdress = new List<string> ();

//	IEnumerator BrokennetworktoDownload ()
//	{
//		for (int i = 0; i < jsonassetbundledata.Count; i++) {
//			string newdatepath = AssetFile + "/" + jsonassetbundledata [i].Path_name; 
//			string date = address + jsonassetbundledata [i].Path_name;
//			if (DownAdress.Contains (date)) {
//			//	Debug.Log("------------"+newdatepath);
//			} else {
//				yield return StartCoroutine (DownLoadAssetbundle (date, LoadType.assetbundletupe));
//			}
//		}
//		if(DownAdress.Count==totalResource)
//		{
//			ComparisonversionMD5 ();
//		}
//		///	ComparisonversionMD5 ();
		
		
//	}
	
	
//	/// <summary>
//	/// Comparisons the M d5.
//	/// </summary>MD5值的生成
//	/// <returns>The M d5.</returns>
//	/// <param name="filepath">Filepath.</param>
//	private string ComparisonMD5 (string filepath)
//	{
//		try {
//			FileStream fs = new FileStream (filepath, FileMode.Open);
//			int len = (int)fs.Length;
//			byte[] data = new byte[len];
//			fs.Read (data, 0, len);
//			fs.Close ();
//			MD5 md5 = new MD5CryptoServiceProvider ();
//			byte[] result = md5.ComputeHash (data);
//			string fileMD5 = "";
//			foreach (byte item in result) {
//				fileMD5 += Convert.ToString (item, 16);
//			}
//			return fileMD5;
//		} catch (FileNotFoundException e) {
//			Debug.Log ("this " + e.Message);
//			return "";
//		}
//	}
	
	
//	/// <summary>
//	/// Downs the load assetbundle.版本号不一致的情况下重新下载
//	/// </summary>
//	IEnumerator  DownLoadAssetbundle (string path, LoadType types)
//	{
//	//	Debug.Log("0000ddddddddd"+path);
//		HTTPRequest request = new HTTPRequest (new Uri (path)).Send ();
//		while (request.State< HTTPRequestStates.Finished) {
//			yield return null;
//		}
//		switch (request.State) {
//		case HTTPRequestStates.Finished:
//			{
//				if (request.Response.IsSuccess) {   // 请求成功
//					switch (types) {
//					case LoadType.txttype:      // 下载下来服务器的版本号
//						byte[] txtdata = request.Response.Data;
//						string txtversiobpath = System.Text.Encoding.ASCII.GetString (txtdata); 
//						JudgeVersion (txtversiobpath);
//					//	Debug.Log (txtversiobpath);
//						txtdata = null;
//						break;
//					case LoadType.jsontype:
//						byte[] jsondata = request.Response.Data;
//						string jsoninfo = System.Text.Encoding.ASCII.GetString (jsondata);  
//						AnalysisJson (jsoninfo);
//						jsondata = null;
//						break;
//					case LoadType.assetbundletupe:
//						yield return StartCoroutine (GreatAssetBundleFile (request.Response.Data, path));
//						progress = progress + (float)(80.0 / totalResource);
//						DownAdress.Add (path);
//						break;
//					}
//				} else {                                             //请求成功完成，但服务器发送错误。
//				ErrorDate(ErrorInfo.server);
//				}
//			}
//			break;
//		case HTTPRequestStates.Error:    // 请求错误
//			{
//			  ErrorDate(ErrorInfo.error);
//			}
//			break;
//		case HTTPRequestStates.Aborted:  // 请求终止
//			{
//			  ErrorDate(ErrorInfo.aborted);
//			}
//			break;
//		case HTTPRequestStates.TimedOut:  // 处理请求超时
//			{
//			  ErrorDate(ErrorInfo.timedout);
//			}
//			break;
//		case  HTTPRequestStates.ConnectionTimedOut://链接超时
//			{
//			  ErrorDate(ErrorInfo.connectiontimedout);
//			}
//			break;
//		}
//	}
	
	
	
	
	
	
//	enum  ErrorInfo
//	{
//		server,
//		error,
//		aborted,
//		timedout,
//		connectiontimedout,
//	}
//	 private void ErrorDate(ErrorInfo info)
//	 {
//	   string textlabel=null;
	 
//	   switch(info)
//	   {
//	    case ErrorInfo.server:
//			textlabel= Writtenwords[70902].dict_cn;
//	    break;
//	    case ErrorInfo.error:
//			textlabel= Writtenwords[70906].dict_cn;
//	    break;
//	    case ErrorInfo.aborted:
//			textlabel= Writtenwords[70907].dict_cn;
//	    break;
//	    case ErrorInfo.timedout:
//			textlabel= Writtenwords[70905].dict_cn;
//	    break;
//	    case ErrorInfo.connectiontimedout:
//			textlabel= Writtenwords[70905].dict_cn;
//	    break;
//	   }
//		AgainTip.gameObject.SetActive(true);
//		Showtiperror.text=textlabel;
//	 }
	 
	 
//	 /// <summary>
//	 /// Buttons the onckil.  点击事件
//	 /// </summary>
//	 /// <param name="obj">Object.</param>
//	public void ButtonOnclick(GameObject obj)
//	 {
//		if(obj.name=="Button")
//	   {
//			AgainTip.gameObject.SetActive(false);
//			Showtiperror.text="";
//		    StartCoroutine(BrokennetworktoDownload ());
//	   }
//	 }
	 
	 
//	 /// <summary>
//	 /// Tabels the manager. 错误信息表
//	 /// </summary>
//	Dictionary<int,DictionaryConfigData> Writtenwords=new Dictionary<int, DictionaryConfigData>();
//	// List<DictionaryConfigData> listdata=new List<DictionaryConfigData>();
//	 private void  TabelManager()
//	 {
//		List<DictionaryConfigData>	 listdata =  DatasManager.getDataInstance().list_DictionaryConfigData;
//		for (int i = 0; i < listdata.Count; i++) {
//			Writtenwords.Add(listdata[i].dict_id,listdata[i]);
//		}
//	 }
	
//}
